using System;

using System.ComponentModel;

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using eWorld.UI;

namespace SBPweb.Controls.Web
{

	/// <summary>
	/// This class implements a date and time selector control
	/// </summary>
	[ToolboxData("<{0}:DateTimeSelector runat=server></{0}:DateTimeSelector>")]
	public class DateTimeSelector : WebControl
	{

		#region Private Members

		private DropDownList mDdlDays = new DropDownList();
		private DropDownList mDdlHours = new DropDownList();
		private DropDownList mDdlMinutes = new DropDownList();
		private CalendarPopup mCalendar = new CalendarPopup();
		private HiddenField mHfDateTime = new HiddenField();

		#endregion

		#region Properties

		/// <summary>
		/// Returns the Calendar popup of the control
		/// </summary>
		public CalendarPopup Calendar
		{
			get
			{
				return mCalendar;
			}
		}

		/// <summary>
		/// Gets or sets the css class of the Days
		/// </summary>
		[Bindable(true)]
		[Category("Appearance")]
		[Description("")]
		public string DaysCssClass
		{
			get
			{
				if (ViewState["DaysCssClass"] == null)
				{
					ViewState["DaysCssClass"] = true;
				}
				return (string)ViewState["DaysCssClass"];
			}

			set
			{
				ViewState["DaysCssClass"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the css class of the hours
		/// </summary>
		[Bindable(true)]
		[Category("Appearance")]
		[Description("")]
		public string HoursCssClass
		{
			get
			{
				if (ViewState["HoursCssClass"] == null)
				{
					ViewState["HoursCssClass"] = true;
				}
				return (string)ViewState["HoursCssClass"];
			}

			set
			{
				ViewState["HoursCssClass"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the css class of the minutes
		/// </summary>
		[Bindable(true)]
		[Category("Appearance")]
		[Description("")]
		public string MinutesCssClass
		{
			get
			{
				if (ViewState["MinutesCssClass"] == null)
				{
					ViewState["MinutesCssClass"] = true;
				}
				return (string)ViewState["MinutesCssClass"];
			}

			set
			{
				ViewState["MinutesCssClass"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the incrementation of minutes
		/// </summary>
		public TimeSpan MinuteIncrement
		{
			get
			{
				if (ViewState["MinuteIncrement"] == null)
				{
					ViewState["MinuteIncrement"] = new TimeSpan(0, 15, 0);
				}
				return (TimeSpan)ViewState["MinuteIncrement"];
			}
			set
			{
				if (value.TotalHours > 1 || 60 % value.TotalMinutes != 0)
				{
					throw new ArgumentException("Only those numbers are valid that are less then 60 and if 60 is divided with them the result is an integer!");
				}
				ViewState["MinuteIncrement"] = value;
			}
		}

		#endregion

		#region Overrided Members

		/// <summary>
		/// Event handler that initializes the control
		/// </summary>
		/// <param name="e">EventArgs e</param>
		protected override void OnInit(EventArgs e)
		{
			mDdlMinutes.Items.Add(new ListItem("00", "0"));
			mDdlMinutes.Items.Add(new ListItem("15", "15"));
			mDdlMinutes.Items.Add(new ListItem("30", "30"));
			mDdlMinutes.Items.Add(new ListItem("45", "45"));

			Controls.Add(mHfDateTime);
			HtmlGenericControl div = new HtmlGenericControl();
			HtmlTable table = new HtmlTable();
			table.CellPadding = 0;
			table.CellSpacing = 0;
			HtmlTableRow tr = new HtmlTableRow();
			HtmlTableCell td = new HtmlTableCell();
			td.Controls.Add(mDdlDays);
			tr.Cells.Add(td);
			td = new HtmlTableCell();
			td.Controls.Add(mCalendar);
			tr.Controls.Add(td);
			td = new HtmlTableCell();
			td.Controls.Add(mDdlHours);
			tr.Controls.Add(td);
			td = new HtmlTableCell();
			td.Controls.Add(mDdlMinutes);
			tr.Controls.Add(td);
			table.Rows.Add(tr);
			div.Controls.Add(table);
			Controls.Add(div);

			base.OnInit(e);
		}

		/// <summary>
		/// Event handler for pre render
		/// </summary>
		/// <param name="e">EventArgs e</param>
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
		}

		#endregion

	}
}
